package com.apop.common.util;

import org.apache.commons.codec.binary.Base64;
import java.io.UnsupportedEncodingException;

/**
 * @ClassName Base64Util
 * @author Jiayi Zheng
 * @date 2021/11/10 15:35
 * @Description: Base64工具
 * @version V1.0
 */
public class Base64Util {

    public final static String ENCODING = "UTF-8";

    /**
     * Base64 加密
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encoded(String data) throws UnsupportedEncodingException {
        byte[] b = Base64.encodeBase64URLSafe(data.getBytes(ENCODING));
        return new String(b, ENCODING);
    }

    /**
     * Base64 解密
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decode(String data) throws UnsupportedEncodingException {
        byte[] b = Base64.decodeBase64(data.getBytes(ENCODING));
        return new String(b, ENCODING);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        // 待加密的字符串
        //String str = "T_PSM_HomeImg/FIcon/1";
        String str = "T_PSM_HomeTheme/FIcon/1";
        // 加密该字符串
        String encodedString = Base64Util.encoded(str);
        System.out.println(encodedString);
        // 解密该字符串
        String decodedString = Base64Util.decode(encodedString);
        System.out.println(decodedString);

    }


}
